//
//  FCImageShow.m
//  commonUse
//
//  Created by WJC on 2018/11/21.
//

#import "FCImageShow.h"
#import "TZImagePickerController.h"
#import "UIImageView+WebCache.h"
#import "SVProgressHUD.h"
#import "WJCDefine.h"
#import "FCTImgBLabelView.h"
#import "UIImageView+WJCDownload.h"
#import "UIView+returnBlock.h"
#import "imageShowBrowser.h"
@interface FCImageShow()<TImgBLabelmageTapDelegate>
/**
 图片展示最大高度//默认80
 */
@property (nonatomic,assign) CGFloat imgMaxHeight;

/**
 单行最多张数
 */
@property (nonatomic,assign) NSInteger defaultCount;
@property (nonatomic,assign) CGFloat imgHeight;
@end
@implementation FCImageShow

-(instancetype)initWithFrame:(CGRect)frame andImageArr:(NSMutableArray *)imageArr andMaxHeight:(CGFloat)imgMaxHeight andDefaultCount:(NSInteger)defaultCount{
    if (self = [super initWithFrame:frame]) {
        self.imgMaxHeight=imgMaxHeight;
        self.defaultCount=defaultCount;
        self.imageArr=imageArr;
        [self refreshImage];
    }
    return self;
}
-(void)refreshImage{
    [self removeChildViews];
    if (self.defaultCount<1) {
        self.defaultCount=3;
    }
    if(self.imgMaxHeight<1){
        if (self.imageArr.count==1) {
            self.imgHeight=150;
        }else self.imgHeight=self.width/self.defaultCount;

    }else{
        if(self.imageArr.count==1){
            self.imgHeight=self.imgMaxHeight;
        }else{
            self.imgHeight=self.width/self.defaultCount;
            if (self.imgHeight>self.imgMaxHeight) {
                self.imgHeight=self.imgMaxHeight;
            }
        }
    }
    
    [_imageViewAry removeAllObjects];
    __block CGFloat x=0,y=0;
    for (int i = 0; i<self.imageArr.count; i++) {
        FCTImgBLabelView *ImgView=[[FCTImgBLabelView alloc] initWithTagFrame:CGRectMake(x, y, self.imgHeight-8, self.imgHeight-8) andImage:self.imageArr[i] andName:nil andHiddenClose:YES];
        ImgView.tag=i;
        ImgView.imageView.tag=i;
        ImgView.imageView.layer.cornerRadius=4;
        ImgView.imageView.layer.masksToBounds=YES;
        [self addSubview:ImgView];
        ImgView.ImgDelegate=self;
        ImgView.label.hidden=YES;
        x+=self.imgHeight;
        if (x+self.imgHeight>self.width&&i<self.imageArr.count-1) {
            x=0;
            y+=self.imgHeight;
        }
        [_imageViewAry addObject:ImgView];
    }
//    [self.imageArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        FCTImgBLabelView *ImgView=[[FCTImgBLabelView alloc] initWithTagFrame:CGRectMake(x, y, self.imgHeight, self.imgHeight) andImage:obj andName:nil andHiddenClose:YES];
//        ImgView.tag=idx;
//        ImgView.imageView.tag=idx;
//        [self addSubview:ImgView];
//        ImgView.ImgDelegate=self;
//        ImgView.label.hidden=YES;
//        x+=self.imgHeight;
//        if (x+self.imgHeight>self.width&&idx<self.imageArr.count-1) {
//            x=0;
//            y+=self.imgHeight;
//        }
//    }];
    self.height=y+self.imgHeight;
    if (self.imageArr.count==0) {
        self.height=y;
    }
}
-(void)TImgBLabelimageWithTap:(UITapGestureRecognizer *)sender AndSuperView:(FCTImgBLabelView *)view{
    imageShowBrowser *browser=[[imageShowBrowser alloc] initWithFrame:CGRectMake(0, 0, WJCScreenW, WJCScreenH) andImageArr:self.imageArr andsmallImage:nil andClickView:sender.view andImgSuperView:self andIndex:sender.view.tag];
    [browser showView];
    if (self.voidBlock) {
        self.voidBlock();
    }
}

- (void)FCImageShowDidClick:(nullable SEL)action{
    for (FCTImgBLabelView *img in _imageViewAry) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
        img.userInteractionEnabled = YES;
        [img addGestureRecognizer:tap];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
